<?php
/**
 * Created by PhpStorm.
 * User: wangkk
 * Date: 2019-06-28 10:20
 * Description: {描述信息}
 */
require_once ('../coupon/common.php');
/*
 使用步骤：
    1、获取template_id
    2、调用微信接口发送消息
    3、参数：
            {
                touser : '',        //接受者openid
                template_id: '',    //模板ID
                url: '',            //点击跳转地址
                miniprogram: {
                    appid: '',      //小程序appid
                    pagepath: '',   //地址，不支持小游戏
                },
                data: {             //模板数据
                    first: {
                       "value":"恭喜你购买成功！",
                       "color":"#173177"
                   },
                   keyword1:{
                       "value":"巧克力",
                       "color":"#173177"
                   },
                }
            }
 */
//接收者openid
$openid = 'oEZwf1IRZc5a9EK-qK7kkUJLe5dw';
//$openid = 'oEZwf1NCUrgf-_M5Q0OhGDmdJlnk';
//模板ID
$templateId = 'AKKfBVF0c5FXNNVyiNUfhN5q18V2Bw2KWa_Upz_qLPs';
//点击跳转地址
$url = 'http://go.alisleepy.top:8081';
//data数据
$data = array(
    'first' => '您收到一张优惠券',
    'keyword1' => array(
        'value' => '【想帮帮】无忧保',
        'color' => "#173177",
    ),
    'keyword2' => array(
        'value' => '99元',
        'color' => "#173177",
    )   ,
    'keyword3' => array(
        'value' => '1123123123123',
        'color' => "#173177",
    ),
    'remark' => array(
        'value' => '点击消息打开，一起赢取更多福利吧！',
        'color' => '#173177'
    ),
);
//接口发送数据
$postData = array(
    'touser' => $openid,
    'template_id' => $templateId,
    'url' => $url,
    'data' => $data,
);
//获取access_token
$access_token = get_access_token();
if (!$access_token) {
    throw new Exception('access_token不能为空');
}
//地址
$postUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
$tplmsgObj = new TemplateMsg($postData, $postUrl);
$res = $tplmsgObj->sendMsg();
if ($res === true) {
    echo '模板消息发送成功'.$res;die;
} else {
    echo '模板消息发送失败，错误码：'.$res;die;
}

//发送微信模板消息
class TemplateMsg{
    public $postData;
    public $postUrl;
    public function __construct($postData = array(), $url = ''){
        if (!$postData['touser']) {
            throw new Exception('用户openid不能为空');
        }
        if (!$postData['template_id']){
            throw new Exception('模板ID不能为空');
        }
        if (!$postData['data']) {
            throw new Exception('模板消息数据不能为空');
        }
        if (!$url) {
            throw new Exception('请求地址不能为空');
        }
        $this->postData = $postData;
        $this->postUrl  = $url;
    }
    public function sendMsg(){
        $data = $this->postData;
        $url = $this->postUrl;
        echo '<pre>';
        print_r($url);
        print_r($data);die;
        // var_dump($url);
        // var_dump($data);die;
        $return = jsonpostJson($url, $data, true); //post请求
        if(intval($return['errcode']) == 0){
            return true;
        }else{
            return $return['errcode'];
        }
    }


}